/*** SPIN ***/

jQuery.fn.spin = function(opts) {
	this.each(function() {
		var $this = $(this),
			data = $this.data();
		
		if (opts !== false) {
			if (data.spinner) {
				data.spinner.stop();
				delete data.spinner;
			}
			data.spinner = new Spinner($.extend({
				lines: 13, 					// The number of lines to draw
				length: 13, 				// The length of each line
				width: 6, 					// The line thickness
				radius: 18, 				// The radius of the inner circle
				corners: 1, 				// Corner roundness (0..1)
				rotate: 0, 					// The rotation offset
				direction: 1, 				// 1: clockwise, -1: counterclockwise
				color: $this.css('color'), 	// #rgb or #rrggbb
				speed: 1.4, 				// Rounds per second
				trail: 40, 					// Afterglow percentage
				shadow: false, 				// Whether to render a shadow
				hwaccel: false, 			// Whether to use hardware acceleration
				className: 'spinner', 		// The CSS class to assign to the spinner
				zIndex: 2e9, 				// The z-index (defaults to 2000000000)
				top: 'auto', 				// Top position relative to parent in px
				left: 'auto' 				// Left position relative to parent in px
			}, opts)).spin(this);
		}
		else{
			data.spinner.stop();
			delete data.spinner;
		}
	});
	return this;
};

/*** CENTER ***/

jQuery.fn.center = function (container) {
	if(typeof container == "undefined")
		container= window;
	
	if($(container).is($(window))){
		return this.each(function(){
			$(this).css("position","absolute");
			$(this).css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() + "px");
			$(this).css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
		});
	}
	else{
		//TODO
	}
}

/*** BACKGROUNDS ***/

jQuery.fn.cover = function (container) {
	if(typeof container == "undefined")
		container= window;
	
	var ww= $(container).width(),
		wh= $(container).height();
	
	return this.each(function(){
		if($(this).is('img')){			
			var target= $(this), 
				img = $('<img>');			
			
			img.load(function(){			
				if(this.width >= this.height){
					target.css('height', wh);
				}else{
					target.css('width', ww);
				}	
				
				target.center();
			})
			
			img.attr('src', $(this).attr('src'));

		}
	});
}

jQuery.fn.contain = function (container) {
	if(typeof container == "undefined")
		container= window;
	
	var ww= $(container).width(),
		wh= $(container).height();
	
	return this.each(function(){
		if($(this).is('img')){			
			$(this).css('max-width', $(window).width());
			$(this).css('max-height', $(window).height());
			$(this).center();
		}
	});
}

/*** CSS ***/

jQuery.fn.css3 = function (prop, val) {
	var prefixes= ['-webkit-', '-moz-', '-o-', '-ms-', ''];
	return this.each(function(){
		for(var index in prefixes){
			$(this).css( prefixes[index]+prop, val);
		}
	});
}

/*** VISIBILITY ***/

jQuery.fn.invisible = function() {
    return this.each(function() {
        $(this).css("visibility", "hidden");
    });
};

jQuery.fn.visible = function() {
    return this.each(function() {
        $(this).css("visibility", "visible");
    });
};


/*** EFFECTS ***/

jQuery.fn.fly = function(duration) {
	if(typeof duration == 'undefined')
		duration= 400;
	
	return this.each(function(){
		var position= $(this).offset(),
			clone= $(this).clone();
		
		clone.css('position', 'absolute');
		clone.css('top', position.top);
		clone.css('left', position.left);
		
		clone.appendTo('body');
		
		clone.width($(this).parent().width());
		clone.height($(this).parent().height());
		
		clone.animate({'top':(0-clone.height()), 'left': ($(window).width()), 'opacity':0.3, 'width': $(this).parent().width()/2, 'height':$(this).parent().height()/2}, duration, 'linear', function(){
			$(this).remove();
		});		
	});
}
